import requests
import json

# 百度句子翻译的URL
url = "https://fanyi.baidu.com/basetrans"
# 要传递的post参数（注意替换为自己浏览器看到的token、sign值）
data = {
    "query": "happy every day",
    "from": "en",
    "to": "zh",
    "token": "ecd953c806496f6f92c2957bb521ccf4",
    "sign": "342945.120976"
}
# 模拟浏览器
headers = {
    "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1",
    # "Cookie": "REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BAIDUID=B4275FC0EC4F7645FDF39C456F05EB6B:FG=1; BDUSS=ZTMzBLUXdlajVNY35-ZUMxMy12d1pWSnNORXFCNEdPRVRXeVhkQzVNaDJsTWRmRUFBQUFBJCQAAAAAAAAAAAEAAAAn4NUTZG9uZ7fJx-8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHYHoF92B6BfVV; BDUSS_BFESS=ZTMzBLUXdlajVNY35-ZUMxMy12d1pWSnNORXFCNEdPRVRXeVhkQzVNaDJsTWRmRUFBQUFBJCQAAAAAAAAAAAEAAAAn4NUTZG9uZ7fJx-8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHYHoF92B6BfVV; BIDUPSID=B4275FC0EC4F7645FDF39C456F05EB6B; PSTM=1604361973; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; PSINO=5; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDRCVFR[X_XKQks0S63]=mk3SLVN4HKm; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; BDRCVFR[tox4WRQ4-Km]=mk3SLVN4HKm; BDRCVFR[CLK3Lyfkr9D]=mk3SLVN4HKm; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1604160877,1604284147,1604302762,1604367163; H_WISE_SIDS=148078_153758_150828_156599_158204_156286_150775_159609_148867_159880_156097_154738_153715_160470_159532_158957_131862_160447_150772_151018_157264_153065_127969_154413_154174_159064_152982_158527_150345_146732_158746_131423_154037_159291_155397_159452_107313_158055_158097_154189_157946_155344_155255_159955_151872_144966_154213_157814_156848_159600_159959_157965_147551_160436_158415_154639_159157_154300_157474_159074_110085_157006; SE_LAUNCH=5%3A26739496; rsv_i=e3ba6vkATHULND4kPJp8%2FeoSF5dbhBhb8OPdu45ahy0HSJn1xVk1zcabOB8W6KoaqpAjat7x2zots0B%2BdklVPUOh9lkCDd4; H_PS_PSSID=32754_1464_32892_32931_32705_7517_32961_32116_26350_32918; Hm_lvt_afd111fa62852d1f37001d1f980b6800=1604307516,1604373337; OUTFOX_SEARCH_USER_ID_NCOO=451446561.4444419; __yjsv5_shitong=1.0_7_e13fd1a73fd33cdd7bbe85747a82e0e9032a_300_1604375027522_49.89.250.81_22b8471b; Hm_lpvt_afd111fa62852d1f37001d1f980b6800=1604375443; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1604375443; yjs_js_security_passport=50ff99fe2b6994ba55519c612c23ba5fc77c40b2_1604375444_js; ___rl__test__cookies=1604375444468"
}

# 字符串cookie（注意替换为自己浏览器中的cookies）
cookies_str = "REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BAIDUID=B4275FC0EC4F7645FDF39C456F05EB6B:FG=1; BDUSS=ZTMzBLUXdlajVNY35-ZUMxMy12d1pWSnNORXFCNEdPRVRXeVhkQzVNaDJsTWRmRUFBQUFBJCQAAAAAAAAAAAEAAAAn4NUTZG9uZ7fJx-8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHYHoF92B6BfVV; BDUSS_BFESS=ZTMzBLUXdlajVNY35-ZUMxMy12d1pWSnNORXFCNEdPRVRXeVhkQzVNaDJsTWRmRUFBQUFBJCQAAAAAAAAAAAEAAAAn4NUTZG9uZ7fJx-8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHYHoF92B6BfVV; BIDUPSID=B4275FC0EC4F7645FDF39C456F05EB6B; PSTM=1604361973; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; PSINO=5; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDRCVFR[X_XKQks0S63]=mk3SLVN4HKm; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; BDRCVFR[tox4WRQ4-Km]=mk3SLVN4HKm; BDRCVFR[CLK3Lyfkr9D]=mk3SLVN4HKm; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1604160877,1604284147,1604302762,1604367163; H_WISE_SIDS=148078_153758_150828_156599_158204_156286_150775_159609_148867_159880_156097_154738_153715_160470_159532_158957_131862_160447_150772_151018_157264_153065_127969_154413_154174_159064_152982_158527_150345_146732_158746_131423_154037_159291_155397_159452_107313_158055_158097_154189_157946_155344_155255_159955_151872_144966_154213_157814_156848_159600_159959_157965_147551_160436_158415_154639_159157_154300_157474_159074_110085_157006; SE_LAUNCH=5%3A26739496; rsv_i=e3ba6vkATHULND4kPJp8%2FeoSF5dbhBhb8OPdu45ahy0HSJn1xVk1zcabOB8W6KoaqpAjat7x2zots0B%2BdklVPUOh9lkCDd4; H_PS_PSSID=32754_1464_32892_32931_32705_7517_32961_32116_26350_32918; Hm_lvt_afd111fa62852d1f37001d1f980b6800=1604307516,1604373337; OUTFOX_SEARCH_USER_ID_NCOO=451446561.4444419; __yjsv5_shitong=1.0_7_e13fd1a73fd33cdd7bbe85747a82e0e9032a_300_1604375027522_49.89.250.81_22b8471b; Hm_lpvt_afd111fa62852d1f37001d1f980b6800=1604375443; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1604375443; yjs_js_security_passport=50ff99fe2b6994ba55519c612c23ba5fc77c40b2_1604375444_js; ___rl__test__cookies=1604375444468"
# 将字符串cookie转换为字典

print(cookies_str.split("; "))
# exit()  # 退出程序

cookies_dict = {temp[:temp.find("=")]: temp[temp.find("=")+1:] for temp in cookies_str.split("; ")}
print(cookies_dict)  # 查看 将字符串的cookies转换为字典形式之后的样子

# exit()

# 发送post请求
r = requests.post(url=url, data=data, headers=headers, cookies=cookies_dict)

print(r.request.headers)  # 查看请求头
# print(r.headers)  # 查看响应头

print(r.text)

text_dict = json.loads(r.text)
print(text_dict.get("errmsg"))

ret = json.loads(r.content.decode())
print(ret["trans"][0]["dst"])

